//
//  BN_ClassParentDynamic.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-3-28.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_ClassParentDynamic.h"
#import <QuartzCore/QuartzCore.h>
#define Radius 12

@implementation BN_ClassParentDynamic

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)init {
    if (self = [super init]) {
        //
    }
    return self;
}

- (void)loadBtnView {
    
    //通知改变按钮状态
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(bottonClickState) name:KSWITCHCLASSPARENT object:nil];
    
    
    // 包装班级/家长动态按钮
    
    self.classBtn = [[UIButton alloc]initWithFrame:CGRectMake(-2, 0, 100, CLASSPARENTVIEWHEIGHT-9)];
    self.classBtn.tag = 1;
    /*0.3607
     0.7647
     0.2196
     */
    UIColor *color = [UIColor colorWithRed:0.29 green:0.81 blue:0.13 alpha:1];
    self.classBtn.backgroundColor = color;
    self.classBtn.layer.cornerRadius = Radius;
    self.classBtn.layer.masksToBounds = YES;
    
    self.parentBtn = [[UIButton alloc]initWithFrame:CGRectMake(102, 0, 100, CLASSPARENTVIEWHEIGHT-9)];
    self.parentBtn.layer.cornerRadius = Radius;
    self.parentBtn.layer.masksToBounds = YES;
    self.parentBtn.tag = 2;
    
    self.classBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.parentBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [self.classBtn setTitle:@"班级动态" forState:UIControlStateNormal];
    [self.classBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.classBtn addTarget:self action:@selector(classClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.parentBtn setTitle:@"家长动态" forState:UIControlStateNormal];
    [self.parentBtn setTitleColor:APP_OBJECT_GREEN_COLOR forState:UIControlStateNormal];
    [self.parentBtn addTarget:self action:@selector(parentClick) forControlEvents:UIControlEventTouchUpInside];
    

    //包装班级动态 和 家长动态 按钮的UIView
    self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-200/2, 5, 200, CLASSPARENTVIEWHEIGHT-9)];
    [self.backgroundView addSubview:self.classBtn];
    [self.backgroundView addSubview:self.parentBtn];
    
    /*self.backgroundView.layer.cornerRadius = 14;
    self.backgroundView.layer.masksToBounds = YES;
    self.backgroundView.layer.borderWidth = 1;
    self.backgroundView.layer.borderColor = [[UIColor colorWithRed:0.3607 green:0.7647 blue:0.2196 alpha:1] CGColor];*/
    
    
    self.backgroundView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.backgroundView];
}

- (void)dynamicClassParentChange:(ClassOrParentListChange)listChange {
    self.listChange = listChange;
}

#pragma mark 按钮的点击方法 
- (void)classClick {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"class" forKey:USERDEFAULTS_SWITCHDYNAMIC];
    [userDefaults synchronize];
    [self bottonClickState];
    self.listChange(YES);
}

- (void)parentClick {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"parent" forKey:USERDEFAULTS_SWITCHDYNAMIC];
    [userDefaults synchronize];
    [self bottonClickState];
    self.listChange(NO);
}

- (void)bottonClickState {
    
    UIColor *color = [UIColor colorWithRed:0.29 green:0.81 blue:0.13 alpha:1];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *state = [userDefaults objectForKey:USERDEFAULTS_SWITCHDYNAMIC];
    if ([state isEqualToString:@"class"]) {
        self.classBtn.backgroundColor = color;
        [self.classBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.parentBtn.backgroundColor = [UIColor clearColor];
        [self.parentBtn setTitleColor:APP_OBJECT_GREEN_COLOR forState:UIControlStateNormal];
        
        //记录“动态”版块 哪个tableView 可见
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setInteger:0 forKey:USERDEFAULTS_CLASSPARENT];
        [userDefaults synchronize];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:KDISPLAYHIDEDYNAMICUIVIEW object:@"display"];
        
        // KDYNAMIC_SCROLLOFFSET
        [[NSNotificationCenter defaultCenter]postNotificationName:KDYNAMIC_SCROLLOFFSET object:nil];

    }
    
    if ([state isEqualToString:@"parent"]) {
        self.parentBtn.backgroundColor = color;
        [self.parentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.classBtn.backgroundColor = [UIColor clearColor];
        [self.classBtn setTitleColor:APP_OBJECT_GREEN_COLOR forState:UIControlStateNormal];
        
        //记录“动态”版块 哪个tableView 可见
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setInteger:1 forKey:USERDEFAULTS_CLASSPARENT];
        [userDefaults synchronize];

        [[NSNotificationCenter defaultCenter]postNotificationName:KDISPLAYHIDEDYNAMICUIVIEW object:@"display"];
        
        // KDYNAMIC_SCROLLOFFSET
        [[NSNotificationCenter defaultCenter]postNotificationName:KDYNAMIC_SCROLLOFFSET object:nil];

    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"test" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"KDYNAMIC_SENDLOGO" object:nil];

}
@end
